import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/entity/home/user_detail_entity.dart';
import 'package:sports_new/entity/match/expert_views_entity.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/server/api/expert_api.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/view/expert/items/plan_item.dart';
import 'package:sports_new/widgets/card.dart';
import 'package:sports_new/widgets/base/qkd_image.dart';

import '../../widgets/base/qkd_text.dart';

class ExpertLeagueListPage extends StatefulWidget {
  const ExpertLeagueListPage({super.key});

  @override
  State<ExpertLeagueListPage> createState() => _ExpertLeagueListPageState();
}

class _ExpertLeagueListPageState extends State<ExpertLeagueListPage> {
  final ExpertLeagueItem? info = Get.arguments;
  List<PlanEntity>? data;
  int _page = 1;
  final EasyRefreshController controller = EasyRefreshController(controlFinishLoad: true);

  int get page => _page;

  set page(int value) {
    setState(() {
      _page = value;
    });
  }

  @override
  void initState() {
    getData();
    super.initState();
  }

  Future getData() async{
    page = 1;
    data = await ExpertApi.getExpertLeaguePlan(info?.expertId, info?.sportsId, info?.leagueId, 1);
    controller.resetFooter();
    setState(() {});
  }

  Future loadMore() async{
    page += 1;
    var result = await ExpertApi.getExpertLeaguePlan(info?.expertId, info?.sportsId, info?.leagueId, page);
    data?.addAll(result);
    controller.finishLoad(
      (data?.length ?? 0) < page*15?IndicatorResult.noMore:IndicatorResult.success
    );
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.greyF2F3F7,
      appBar: Styles.appBar(title: QkdText(info?.expertName ?? "")),
      body: Column(
        children: [
          Row(
            children: [
              QkdImage(
                imageUrl: info?.leagueLogo,
                width: 28,
                height: 28,
                fit: BoxFit.cover,
                placeholder: Styles.placeholderIcon(),
                errorWidget: Styles.placeholderIcon(),
              ),
              Container(width: 4),
              QkdText(info?.leagueName ?? "",fontWeight: FontWeight.w500),
              const Spacer(),
              QkdText(info?.content ?? "",fontWeight: FontWeight.w500,),
            ],
          ).paddingSymmetric(horizontal: 8,vertical: 17),
          Expanded(
            child: EasyRefresh(
              controller: controller,
              onRefresh: getData,
              onLoad: loadMore,
              header: Styles.refreshHeader(),
              footer: Styles.refreshFooter(position: IndicatorPosition.locator),
              child: MediaQuery.removePadding(
                context: context,
                removeTop: true,
                child: ListView(
                  children: data?.map<Widget>(
                    (e) => Block(
                      cardStyle: const CardStyle(
                          margin: EdgeInsets.fromLTRB(8, 0, 8, 8),
                          padding: EdgeInsets.symmetric(horizontal: 12)
                      ),
                      child: PlanItem(entity: e,isExpertDetailView: true))
                  ).toList().append(const FooterLocator()) ?? []
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}
